﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mitasoft.Books.Client.Desktop
{
    internal class ServiceManager
    {
        private static HashSet<object> servicesHash;
        
        //private static BooksDiscoveryService discoveryService = null;
        //private static Services.ApplicationMessagesService appMessagesService = null;

        /// <summary>
        /// static ctor
        /// </summary>
        static ServiceManager()
        {
            //discoveryService = new Services.BooksDiscoveryService();
            //appMessagesService = new Services.ApplicationMessagesService();

            servicesHash = new HashSet<object>();
            
            //servicesHash.Add(discoveryService);
            //servicesHash.Add(appMessagesService);
        }

        /// <summary>
        /// Init the service manager
        /// </summary>
        public static void Init()
        {
            if (servicesHash == null || servicesHash.Count == 0)
                throw new Exception("Service unable to init !");
        }

        /// <summary>
        /// Add a service
        /// </summary>
        /// <param name="service"></param>
        public static void AddService(object service)
        {
            servicesHash.Add(service);
        }

        /// <summary>
        /// Retrive a service of type T
        /// </summary>
        /// <typeparam name="T">service type</typeparam>
        /// <returns></returns>
        public static T GetService<T>()
        {
            foreach (var service in servicesHash)
            {
                if (service is T)
                    return (T)service;
            }

            return default(T);
        }

    }
}
